package leetcode;

public class C7007 {
    public static void main(String[] args) {
        int [][]grid=new int [][]{{1,1,1,0},
                {1,0,1,0},
                {0,1,1,0},
                {0,1,0,0}};
        int i,j,num=0;
        for(i=0;i< grid.length;i++){
            for(j=0;j< grid[0].length;j++){
                if (grid[i][j]==1) {

                  num=num+perimeter(grid,i,j);

                }

            }
        }
        System.out.println(num);

    }


    public static int perimeter(int[][]grid, int i,int j){
        int a=4;
        if(i>0&&i< grid.length&&grid[i-1][j]==1){
            a=a-1;
        }if(i>=0&&i< grid.length-1&&grid[i+1][j]==1){
            a=a-1;
        }if(j>=0&&j< grid[0].length-1&&grid[i][j+1]==1){
            a=a-1;
        }if(j>0&&j< grid[0].length&&grid[i][j-1]==1){
            a=a-1;
        }
    return a;
    }



}
